<?php
namespace app\addons\controller;
// use app\index\
class Partner extends Base
{
    public function index()
    {
        $keywords=request()->param('keywords');       
        $article=db("plugin_partner");
        $where['status']=1;
        if($keywords){
            $where['name']=['like', '%'.$keywords.'%'];
        }
        // print_r($where);exit;
        // $where['type']=1;
        $count=$article->where($where)->count();
        $data=$article->where($where)->order("is_top desc,sort asc,id desc")->select();


        $this->assign("mess",$data);
        $this->assign("count",$count);
        return view('index');
    }
    public function index_del(){
        $id=$_GET['id'];
        $res=db("plugin_partner")->delete($id);
        if($res){
            $mess['code']=1;
            $mess['msg']="删除成功";
        }else{
            $mess['code']=0;
            $mess['msg']="删除失败";
        }       // print_r($data);exit;
        return json_encode($mess);
    }
    public function addMess()
    {
        // print_r(strtotime("2018-06-15 00:00:00"));
        // print_r(date("Y-m-d H:i:s",1528992000));exit;
        $id=request()->param('id');
        $fid=request()->param('fid');
        if($id){
            $mess=db("plugin_partner")->where("id",$id)->find();
            if($mess['images']){
                $mess['photos']=explode(",",$mess['images']);  
            }
            // if($data[$i]['filex']){
            //     $data[$i]['viewfilex']=db("filex")->where("id=".$data[$i]['filex'])->value("filex");  
            // }
            $mess['createtime']=date("Y-m-d H:i:s",$mess['createtime']);
            $this->assign("mess",$mess);
        }
        // print_r($mess);exit;
        $this->assign("fid",$fid);
        return view('addMess');
    }
    public function addMessDone()
    {
        $updid=request()->param('updid'); 
        $data=$_POST;

        if($data['is_home'] == 'on'){
            $data['is_home']=1;
        }else{
            $data['is_home']=0;
        }
        if($data['is_top'] == 'on'){
            $data['is_top']=1;
        }else{
            $data['is_top']=0;
        }
        if($data['is_hot'] == 'on'){
            $data['is_hot']=1;
        }else{
            $data['is_hot']=0;
        }
        $data['aid']=$this->userMsg['id'];
        if($data['createtime']){
            $data['createtime']=strtotime($data['createtime']);
        }else{
            $data['createtime']=time();
        }

        if($updid){
            $data['updatetime']=time();
            $res=db("plugin_partner")->where("id",$updid)->update($data);
        }else{                 
            $res=db("plugin_partner")->insert($data);
        }
        // $images=explode(',',$data['images']);
        // $where['status']=1;
        // $count=db("modelx")->where($where)->count();
        // $data=db("modelx")->where($where)->select();
        if($res){
            $mess['code']=200;
            $mess['msg']='保存成功';
        }else{
            $mess['code']=100;
            $mess['msg']='保存失败';
        }        
        return json_encode($mess);
    }
    public function indexSort(){
        $id=request()->param('id');            
        $sort=request()->param('sort');

        $data['sort']=$sort;
        $res=db("plugin_partner")->where("id",$id)->update($data);
        if($res){
            $mess['code']=1;
            $mess['msg']="操作成功";
        }else{
            $mess['code']=0;
            $mess['msg']="操作失败";
        }
        return json_encode($mess);   
    }
}
